<?php
/**
 * 门店管理
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019-03-28
 * Time: 下午 4:28
 */
namespace App;

use Model\StoreManageMod;
use Model\StoreMod;
use Model\StorePositionMod;
use Model\StoreStaffMod;

class Store extends Action
{
    private $Store = null;   //门店
    private $StoreStaff = null;  //员工
    private $StoreManage = null;  //系统的经营范围
    private $StorePosition = null; //门店职位

    public function __construct()
    {
        parent::__construct();
        $this->Store = new StoreMod();
    }

    //----S前端门店管理端-----------

    /**
     * 通过用户id获取门店信息
     * @param uid  用户id
     */
    public function store()
    {
        $this->StoreStaff = new StoreStaffMod();
        $_store_staff = $this->StoreStaff->findOne(array('uniacid' => $this->_uniacid, 'uid' => $this->_gpc['uid']), array('store_id', 'position_id'));
        if ($_store_staff) {
            $_store = $this->Store->findOne(array('id' => $_store_staff['store_id'],"uniacid"=>$this->_uniacid,'status !=' => 9), array('name', 'logo', 'address', 'sell_range', 'status','location','create_time'));
            if ($_store) {
                $_store['sell_range'] = $this->strManageToArray($_store['sell_range']);
                $_store['position_id'] = abs($_store_staff['position_id']);
                $_store['store_id'] = abs($_store_staff['store_id']);
                $_store['authority'] = $this->positionIdToAuthority($_store['position_id']);
                $_store['position'] = $this->positionIdToNmae($_store['position_id']);
                $_store['location'] = $_store['location'] != '' ? json_decode($_store['location'],true) : array();
                $_store['create_time'] = $_store['create_time'] == 1 ? strtotime(date('2019-11-01')) : $_store['create_time'];
                $this->_result = array('status' => 'success', 'data' => $_store);
            }
        }
        $this->result($this->_result);
    }

    /**
     * 新增门店
     * @param uid  用id
     * @param data[name,logo,address,sell_range]
     */
    public function add()
    {
        $this->_gpc['data']['uniacid'] = $this->_uniacid;
        $this->_gpc['data']['create_time'] = TIMESTAMP;
        $_result = $this->Store->insert($this->_gpc['data']);
        if ($_result) {
            $_store_id = pdo_insertid();
            $this->StoreStaff = new StoreStaffMod();
            $this->StoreStaff->insert(array(
                'uniacid' => $this->_uniacid,
                'store_id' => $_store_id,
                'uid' => $this->_gpc['uid']
            ));
            $this->_result = array('status' => 'success', 'data' => $_store_id);
        }
        $this->result($this->_result);
    }

    /**
     * 修改门店
     */
    public function edit()
    {
        $_result = $this->Store->update($this->_gpc['data'],array('id'=>$this->_gpc['store_id']));
        if ($_result) $this->_result = array('status'=>'success');
        $this->result($this->_result);
    }
    //----E前端门店管理端-----------


    //----S微擎后台------------
    /**
     * 获取门店总数
     * @param $_status //门店状态
     */
    public function getTotal()
    {
        $_where = array('uniacid' => $this->_uniacid, 'status' => $this->_gpc['status']);
        if($_search_where = $this->searchWhere()){
            $_where = array_merge($_where,$_search_where);
            unset($_search_where);
        }
        $_total = $this->Store->total($_where);
        $this->_result = array('status' => 'success', 'data' => $_total);
        $this->result($this->_result);
    }

    /**
     * 列表
     * @param status //门店状态
     * @param limit  //请求下标数量
     */
    public function li()
    {
        $_where = array('uniacid' => $this->_uniacid, 'status' => $this->_gpc['status']);
        if($_search_where = $this->searchWhere()){
            $_where = array_merge($_where,$_search_where);
            unset($_search_where);
        }
        $_list = $this->Store->findAll($_where, array('order' => array('id', 'desc'), 'limit' => $this->_gpc['limit']), array('id', 'name', 'logo', 'address', 'sell_range'));
        if (count($_list) > 0) {
            foreach ($_list as $_k => $_v) {
                $_list[$_k]['sell_range'] = $this->strManageToArray($_v['sell_range']);
                $_list[$_k]['logo'] = $this->_w['attachurl_local'] . $_v['logo'];
            }
            $this->_result = array('status' => 'success', 'data' => $_list);
        }
        $this->result($this->_result);
    }

    /**
     * 搜索门店关键词返回where
     */
    private function searchWhere()
    {
        $_where = array();
        if(isset($this->_gpc['search_keyword'])){
            if ($this->_gpc['search_type'] == 'id'){
                $_where['id'] = $this->_gpc['search_keyword'];
            } else {
                $_where['name like'] = '%'.$this->_gpc['search_keyword'].'%';
            }
        }else{
            $_where = false;
        }
        return $_where;
    }

    /**
     * 更改门店状态
     * @param store_id  门店id
     * @param status 更改的状态
     */
    public function changeStatus()
    {
        $_result = $this->Store->update(array('status'=>$this->_gpc['status']),array('id'=>$this->_gpc['store_id']));
        if($_result) $this->_result = array('status'=>'success');
        $this->result($this->_result);
    }
    //----E微擎后台------------


    //------私有方法---------
    /**
     * 通过门店表内的字符串经营范围id   1,2,3,4  转换为数组  array('id'=>1,'name'=>'童装')
     * @param $_str_manage
     */
    private function strManageToArray($_str_manage)
    {
        if(!$this->StoreManage instanceof StoreManageMod) $this->StoreManage = new StoreManageMod();
        $_sell_range_arr = explode(',',$_str_manage);
        $_sell_range = array();
        foreach($_sell_range_arr as $_value){
            $_store_manage = $this->StoreManage->findOne(array('id'=>$_value),array('name'));
            $_sell_range[] = array('id'=>$_value,'name'=>$_store_manage['name']);
        }
        unset($_sell_range_arr);
        return $_sell_range;
    }

    /**
     * 通过职位id返回职位拥有的权限
     * @param $_position_id
     */
    private function positionIdToAuthority($_position_id)
    {
        if(!$this->StorePosition instanceof StorePositionMod) $this->StorePosition = new StorePositionMod();
        $_position = $this->StorePosition->findOne(array('id'=>$_position_id),array('authority'));
        return json_decode($_position['authority'],true);
    }

}